﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AL100.Samples.Polymorphisme
{
    class Program
    {
        static void Main(string[] args)
        {
            //Test_Poly_Heritage();
            Test_Poly_Interfaces();

        }

        /// <summary>
        /// Démonstration du polymorphisme via l'héritage.
        /// </summary>
        private static void Test_Poly_Heritage()
        {
            // Type déclaration : Vehicle, Type instancié : Vehicle.
            Vehicle v = new Vehicle();
            Console.WriteLine("{0}.{1} ==> {2} km/h",
                              v.GetType().ToString(),
                              "GetMaxSpeeed",
                              v.GetMaxSpeed());

            // Type déclaration : Clio, Type instancié : Clio.
            Clio c = new Clio();
            Console.WriteLine("{0}.{1} ==> {2} km/h",
                              c.GetType().ToString(),
                              "GetMaxSpeeed",
                              c.GetMaxSpeed());


            // Comportement polymorphique via l'héritage.
            // Type déclaration : Vehicle, Type instancié : Clio.
            // Méthode appelée : Clio.GetMaxSpeed.
            Vehicle v1 = new Clio();
            Console.WriteLine("{0}.{1} ==> {2} km/h",
                              v1.GetType().ToString(),
                              "GetMaxSpeeed",
                              v1.GetMaxSpeed());


            // Comportement polymorphique via l'héritage.
            // Type déclaration : Vehicle, Type instancié : AstonMartin.
            // Méthode appelée : Clio.GetMaxSpeed.
            Vehicle v2 = new AstonMartin();
            Console.WriteLine("{0}.{1} ==> {2} km/h",
                              v2.GetType().ToString(),
                              "GetMaxSpeeed",
                              v2.GetMaxSpeed());
        }

        private static void Test_Poly_Interfaces()
        {
            /* On crée un tableau de type IPassengerCounter             
             */
            IPassengerCounter[] counters = new IPassengerCounter[2];

            counters[0] = new Clio();
            counters[1] = new AstonMartin();

            foreach (var passengerCounter in counters)
            {
                Console.WriteLine("{0}.{1} ==> {2} passenger(s)",
                             passengerCounter.GetType().ToString(),
                             "GetMaxPassenger",
                             passengerCounter.GetMaxPassenger());
            }

        }

    }


    public class Vehicle
    {
        public virtual int GetMaxSpeed()
        {
            return 0;
        }
    }

    public class AstonMartin : Vehicle, IPassengerCounter
    {
        public override int GetMaxSpeed()
        {
            return 350;
        }

        public int GetMaxPassenger()
        {
            return 2;
        }
    }


    public class Clio : Vehicle, IPassengerCounter
    {
        public override int GetMaxSpeed()
        {
            return 170;
        }

        public int GetMaxPassenger()
        {
            return 4;
        }
    }


    public interface IPassengerCounter
    {
        int GetMaxPassenger();
    }


}
